package com.wl156.adapters;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wl156.lite.R;

public class GoodAdapter extends CheckableAdapter implements OnClickListener {

	public GoodAdapter(Context context, SoapObject data) {
		this(context, data, DEFAULT);
	}

	public GoodAdapter(Context context, SoapObject data, int type) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		mContext = context;
		mType = type;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_good, null);
		}

		TextView from_to = (TextView) convertView.findViewById(R.id.from_to);
		TextView name = (TextView) convertView.findViewById(R.id.name);
		TextView distance = (TextView) convertView.findViewById(R.id.distance);

		TextView lengthModelWeight = (TextView) convertView
				.findViewById(R.id.lengthModelWeight);
		TextView xinyongscore = (TextView) convertView
				.findViewById(R.id.xinyongscore);
		TextView setOffDate = (TextView) convertView
				.findViewById(R.id.setOffDate);

		TextView phoneDial = (TextView) convertView.findViewById(R.id.dial);
		phoneDial.setOnClickListener(this);

		int phoneIndex = 0;
		switch (mType) {
		case DEFAULT:
			from_to.setText(String.format("%s=>%s",
					getTextViewStringFromProperty(position, 1),
					getTextViewStringFromProperty(position, 2)));
			setTextViewStringFromProperty(position, name, 3);
			setTextViewStringFromProperty(position, distance, 12);

			lengthModelWeight.setText(String.format("%s %s %s",
					getTextViewStringFromProperty(position, 6),
					getTextViewStringFromProperty(position, 5),
					getTextViewStringFromProperty(position, 4)));
			setTextViewStringFromProperty(position, xinyongscore, 11);
			setTextViewStringFromProperty(position, setOffDate, 10);

			phoneIndex = 8;
			break;
		case COLLECT:
			from_to.setText(String.format("%s=>%s",
					getTextViewStringFromProperty(position, 2),
					getTextViewStringFromProperty(position, 3)));
			setTextViewStringFromProperty(position, name, 4);
			setTextViewStringFromProperty(position, distance, 13);

			lengthModelWeight.setText(String.format("%s %s %s",
					getTextViewStringFromProperty(position, 7),
					getTextViewStringFromProperty(position, 6),
					getTextViewStringFromProperty(position, 5)));
			setTextViewStringFromProperty(position, xinyongscore, 12);
			setTextViewStringFromProperty(position, setOffDate, 11);

			phoneIndex = 9;
			break;
		}
		setPhoneView(position, phoneDial, phoneIndex);

		super.getView(position, convertView, parent);

		if (position % 2 != 0) {
			convertView.setBackgroundColor(Color.parseColor("#F0FAFE"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
		}

		return convertView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dial:
			Intent i = new Intent(Intent.ACTION_CALL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			mContext.startActivity(i);
			break;
		}
	}
}
